<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px"
    >
      <el-form-item label="折扣方案" prop="discountId">
        <el-select :disabled="updateDisabled" @change="listSelectedFoods" v-model="dataForm.discountId">
          <el-option v-for="item in disList" :label="item.discountName" :key="item.discountId" :value="item.discountId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择菜品" prop="foodList">
        <div v-for="foo in foodList">
          <el-tag type="success">{{ foo.foodTypeName }}</el-tag>
          <el-checkbox-group v-show="dataForm.id == 0" v-model="dataForm.foodList">
            <el-checkbox v-for="f in foo.foodList" :key="f.id" :label="f.id">{{ f.foodName }}</el-checkbox>
          </el-checkbox-group>
          <el-checkbox-group v-show="dataForm.id != 0" v-model="dataForm.foodList" :disabled="true">
            <el-checkbox v-for="f in foo.foodList" :key="f.id" :label="f.id">{{ f.foodName }}</el-checkbox>
          </el-checkbox-group>
        </div>
      </el-form-item>
      <el-form-item label="折扣(请输入1-10之间的数字,10代表不打折)" prop="discount" label-width="300px">
        <el-input-number :min="1" :max="10" style="width: 100%" v-model="dataForm.discount"
                         placeholder="折扣(请输入1-10之间的数字)"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
		</span>
  </el-dialog>
</template>

<script>
import { listFoods } from '@/api/food/food'

export default {
  data() {
    return {
      updateDisabled: false,
      disList: [],
      foodList: [],
      visible: false,
      disabledSubmit: false,
      dataForm: {
        id: 0,
        discountId: '',
        foodId: '',
        discount: '',
        foodList: []
      },
      dataRule: {
        discountId: [{ required: true, message: '折扣方案ID不能为空', trigger: 'blur' }],
        foodList: [{ required: true, message: '菜品ID不能为空', trigger: 'blur' }],
        discount: [{ required: true, message: '折扣不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {

    //已经添加过的菜品
    listSelectedFoods() {
      this.dataForm.foodList = []
      this.$http({
        url: this.$http.adornUrl('/store/tstorediscountdetail/alreadyAddFoods/' + this.dataForm.discountId),
        method: 'get'
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.foodList = data.foodList
        } else {
          this.dataForm.foodList = []
        }
      })
    },

    init(id) {
      listFoods().then(response => {
        this.foodList = response.data
        let m = new Map(this.foodList)
        console.log(m, 'm')
      })

      /*  //菜品列表
       this.$http({
         url: this.$http.adornUrl('/store/tstorefood/listFood'),
         method: 'get'
       }).then(({ data }) => {
         if (data && data.code === 0) {
           this.foodList = data.foodList
         } else {
           this.foodList = []
         }
       }) */

      /*  //折扣方案
       this.$http({
         url: this.$http.adornUrl('/store/tstorediscount/list'),
         method: 'get',
         params: this.$http.adornParams({
           status: 1
         })
       }).then(({ data }) => {
         if (data && data.code === 0) {
           this.disList = data.page.list
         } else {
           this.disList = []
         }
       }) */

      this.dataForm.id = id || 0
      if (id !== 0) {
        this.updateDisabled = true
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        /* if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/store/tstorediscountdetail/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.discountId = data.tStoreDiscountDetail.discountId
              this.dataForm.foodList = data.tStoreDiscountDetail.foodList
              this.dataForm.discount = data.tStoreDiscountDetail.discount
            }
          })
        } */
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.disabledSubmit = true
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/store/tstorediscountdetail/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              discountId: this.dataForm.discountId,
              foodList: this.dataForm.foodList,
              discount: this.dataForm.discount
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.disabledSubmit = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.disabledSubmit = false
              this.$message.error(data.msg)
            }
          })
        } else {
          this.disabledSubmit = false
        }
      })
    }
  }
}
</script>
